//SPDX-FileCopyrightText: Copyright 2022-2024 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package events_api_serv

import (
	"context"

	"atomgit.com/openlinksaas/api-server/dao"
	"atomgit.com/openlinksaas/api-server/dao/event_dao"
	"atomgit.com/openlinksaas/api-server/dao/project_dao"
	"atomgit.com/openlinksaas/api-server/utils"
	"atomgit.com/openlinksaas/proto-gen-go.git/events_api"
	"go.mongodb.org/mongo-driver/mongo"
)

func (apiImpl *EventsApiImpl) SetDayAddonInfo(ctx context.Context, req *events_api.SetDayAddonInfoRequest) (*events_api.SetDayAddonInfoResponse, error) {
	sessionItem, ok := dao.CacheDao.GetSession(ctx, req.SessionId)
	if !ok {
		return &events_api.SetDayAddonInfoResponse{
			Code:   events_api.SetDayAddonInfoResponse_CODE_WRONG_SESSION,
			ErrMsg: "会话不存在",
		}, nil
	}
	projectItem, _, _, err := utils.GetProjectMemberRole(ctx, req.ProjectId, sessionItem.UserId)
	if err == utils.ErrNoProject {
		return &events_api.SetDayAddonInfoResponse{
			Code:   events_api.SetDayAddonInfoResponse_CODE_NO_PROJECT,
			ErrMsg: "项目不存在",
		}, nil
	} else if err == utils.ErrNoRole {
		return &events_api.SetDayAddonInfoResponse{
			Code:   events_api.SetDayAddonInfoResponse_CODE_NO_PERMISSION,
			ErrMsg: "没有权限",
		}, nil
	} else if err != nil {
		return nil, err
	}
	if projectItem.Closed {
		return &events_api.SetDayAddonInfoResponse{
			Code:   events_api.SetDayAddonInfoResponse_CODE_NO_PERMISSION,
			ErrMsg: "没有权限",
		}, nil
	}

	dbSess, err := dao.StartSession()
	if err != nil {
		return nil, err
	}
	defer dao.EndSession(dbSess)
	ret, err := dbSess.WithTransaction(ctx, func(sessCtx mongo.SessionContext) (interface{}, error) {
		err = event_dao.DayAddonInfoDao.Upsert(sessCtx, &event_dao.DayAddonInfoDbItem{
			ProjectId: req.ProjectId,
			UserId:    sessionItem.UserId,
			Day:       req.Day,
			Content:   req.Content,
		})
		if err != nil {
			return nil, err
		}
		return &events_api.SetDayAddonInfoResponse{
			Code: events_api.SetDayAddonInfoResponse_CODE_OK,
		}, nil
	})
	if err != nil {
		return nil, err
	}
	return ret.(*events_api.SetDayAddonInfoResponse), nil
}

func (apiImpl *EventsApiImpl) GetDayAddonInfo(ctx context.Context, req *events_api.GetDayAddonInfoRequest) (*events_api.GetDayAddonInfoResponse, error) {
	emptyInfo := &events_api.DayAddonInfo{}
	sessionItem, ok := dao.CacheDao.GetSession(ctx, req.SessionId)
	if !ok {
		return &events_api.GetDayAddonInfoResponse{
			Code:   events_api.GetDayAddonInfoResponse_CODE_WRONG_SESSION,
			ErrMsg: "会话不存在",
			Info:   emptyInfo,
		}, nil
	}
	exist, err := project_dao.MemberInfoDao.Exist(ctx, req.ProjectId, sessionItem.UserId)
	if err != nil {
		return nil, err
	}
	if !exist {
		return &events_api.GetDayAddonInfoResponse{
			Code:   events_api.GetDayAddonInfoResponse_CODE_NO_PERMISSION,
			ErrMsg: "没有权限",
			Info:   emptyInfo,
		}, nil
	}
	exist, err = event_dao.DayAddonInfoDao.Exist(ctx, req.ProjectId, req.Day, sessionItem.UserId)
	if err != nil {
		return nil, err
	}
	if !exist {
		return &events_api.GetDayAddonInfoResponse{
			Code: events_api.GetDayAddonInfoResponse_CODE_OK,
			Info: &events_api.DayAddonInfo{
				ProjectId: req.ProjectId,
				UserId:    sessionItem.UserId,
				Day:       req.Day,
				Content:   "",
			},
		}, nil
	}
	addonItem, err := event_dao.DayAddonInfoDao.Get(ctx, req.ProjectId, req.Day, sessionItem.UserId)
	if err != nil {
		return nil, err
	}

	return &events_api.GetDayAddonInfoResponse{
		Code: events_api.GetDayAddonInfoResponse_CODE_OK,
		Info: addonItem.ToDayAddonInfo(),
	}, nil
}
